<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-form-item label="姓名" prop="name">
            <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
            <el-input v-model="ruleForm.mobile" maxlength="11" show-word-limit />
        </el-form-item>
        <el-form-item label="选择角色" prop="role_id">
            <component :is="roleSelect" :data=ruleForm.role_id @onRoleSelect="onRoleSelect" />
        </el-form-item>
        <el-form-item label="设置密码" prop="password">
            <el-input v-model="ruleForm.password" />
            <el-text size="small">不填则默认为:123456</el-text>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
            <el-radio-group v-model="ruleForm.sex">
                <el-radio :value="1" size="large">男</el-radio>
                <el-radio :value="0" size="large">女</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="入场时间" prop="start_date">
            <el-date-picker v-model="ruleForm.start_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="出场时间" prop="end_date">
            <el-date-picker v-model="ruleForm.end_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="考勤" prop="work">
            <el-input v-model="ruleForm.work" />
        </el-form-item>
        <el-form-item label="岗位证书及编号" prop="cert">
            <el-input v-model="ruleForm.cert" />
        </el-form-item>
        <el-form-item label="紧急联系人/电话" prop="contacts">
            <el-input v-model="ruleForm.contacts" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
            <el-input v-model="ruleForm.remarks" />
        </el-form-item>
    </el-form>
    <div class="mt20 text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import roleSelect from "./roleSelect.vue";
import http from '../../../../util/request';
const loading = ref<boolean>(false)

interface RuleForm {
    id:number
    name: string
    mobile: string
    password:string
    role_id:number
    sex:number
    start_date:string
    end_date:string
    post:string
    work:number
    cert:string
    contacts:string
    remarks:string
} 

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id:0,
    name: '',
    mobile: '',
    password:'',
    role_id:0,
    sex:1,
    start_date:'',
    end_date:'',
    post:'/',
    work:0,
    cert:'/',
    contacts:'/',
    remarks:'/'
})

const onGetData = () => {
    if(!ruleForm.value.id){
        return
    }
    http.get('/user/'+ruleForm.value.id).then((response:any)=>{
        ruleForm.value = response.data.data
    })
}

const props = defineProps<{
    data:number
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    ruleForm.value.name = ''
    ruleForm.value.mobile = ''
    ruleForm.value.password = ''
    ruleForm.value.role_id = 0
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    name: [
        { required: true, message: '请输入名称', trigger: 'blur' },
        { min: 2, max: 30, message: '范围在2-30字之间', trigger: 'blur' },
    ],
    mobile: [
        { required: true, message: '请输入手机号码', trigger: 'blur' },
        { min: 11, max: 11, message: '请输入正确格式的手机号码', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/user',ruleForm.value)
            }else{
                request = http.put('/user',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);return
                }
                ElMessage.success(response.data.msg);
                saveUser()  
            })
                      
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onRoleSelect = (data:number) => {
    ruleForm.value.role_id = data
}

const saveUser = () => {
    emit('saveUser')
}

const emit = defineEmits(['saveUser'])
</script>
<style scoped>
</style>
